#!/usr/bin/python

#-------------------------------------------------------------------------------
# Name:        loadconfig
# Purpose:     reading the main configuration file.
#
# Author:      Donald L Talbert , www.bwhgaming.com
#
# Created:     20/09/2012
# Copyright:   (c) Donald L Talbert 2012
# Licence:     MIT
#-------------------------------------------------------------------------------

import ConfigParser
import logging

class LoadConfig(object):
    """
    This class handles all the loading and storing of values in the
    configuration file.
    """
    def __init__(self):
        """
        Sets up the ConfigParser on load.
        Defines class varables
        Loads varables values
        """
        # Setting up configparser
        self.config = ConfigParser.RawConfigParser()
        self.config.read('config.ini')

        # Declaring class varables
        self.listServPaths = None
        self.restartTime = None
        self.numServers = None

        self.GameName = None
        self.maxBackups = None
        self.serverDirectory = None
        self.backupDirectory = None
        self.timeBetweenBackups = None
        self.compressionType = None

        self.steamcmdDirectory = None
        self.updateDirectory = None
        self.command = None
        self.listLoadUpdate = None

        self.firstRun = None


        # Loading class varables
        self.loadServers()
        self.loadOptions()
        self.loadBackup()
        self.loadUpdate()
        self.loadMisc()

    def loadServers(self, serverNum = 0): #This could be improved.
        """
        Returns a single server path or a list of all server paths.
        Also sets varables

        """
        if serverNum != 0:
            return self.config.get('Servers', str(serverNum))
        else:
            pass

        self.listServPaths = [self.config.get('Servers', '1'), self.config.get('Servers', '2')]
        self.numServers = len(self.config.options('Servers'))
        return self.listServPaths


    def loadOptions(self):
        """
        returns the time in a list format.
        Sets a varable.

        """
        rawTime = self.config.get('Options', 'restart-time')
        self.restartTime = str(rawTime).split(':') # Converting the time to the format we need.
        return self.restartTime


    def loadBackup(self):
        """
        sets varables for steamcmd directory, update directory, time between backups, and the command.
        Also can return them in a list.

        """
        self.GameName = self.config.get('Backup', 'game-name')
        self.maxBackups = self.config.get('Backup', 'max-backups')
        self.serverDirectory = self.config.get('Backup', 'server-directory')
        self.backupDirectory = self.config.get('Backup', 'backup-directory')
        self.timeBetweenBackups = int(self.config.get('Backup', 'time-between-backups')) * 3600 #Convert hours into seconds for time.sleep()
        self.compressionType = self.config.get('Backup', 'compression-type')
        self.listLoadBackup = [self.GameName, self.maxBackups, self.serverDirectory, self.backupDirectory, self.timeBetweenBackups, self.compressionType]
        return self.listLoadBackup


    def loadUpdate(self):
        """
        sets varables for steamcmd directory, update directory, and the command.
        Also can return them in a list.

        """
        self.steamcmdDirectory = self.config.get('Update', 'steamcmd-directory')
        self.updateDirectory = self.config.get('Update','update-directory')
        self.command = self.config.get('Update','command')
        self.timeBetweenUpdates = int(self.config.get('Update','time-between-updates')) * 3600 #Convert hours into seconds for time.sleep()
        self.listLoadUpdate = [self.steamcmdDirectory, self.updateDirectory, self.command, self.timeBetweenUpdates]
        return self.listLoadUpdate

    def loadMisc(self):
        config = ConfigParser.RawConfigParser()
        config.read('misc.ini')
        self.firstRun = config.get('Misc', 'first-run')
        return self.firstRun


if __name__ == '__main__':
    pass
